package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    //查找所有role信息
    public List<Role> findAll() {
        return roleDao.findAll();
    }
    //添加基本信息,以及role和user表的关联信息
    public void add(Role role, Integer[] userIds, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        Integer roleId = role.getId();//获得roleid
        //设置三个多对多关系.
        this.setRoleAndUser(roleId, userIds);
        this.setRoleAndPermission(roleId, permissionIds);
        this.setRoleAndMenu(roleId, menuIds);
    }



    public List<Integer> findRoleIdsByUserId(Integer roleIds) {
        return roleDao.findRoleIdsByUserId(roleIds);

    }

    @Override
    public void deleteById(Integer id) {
        //先删除所有关联表信息
        roleDao.deleteMenuAssociation(id);
        roleDao.deletePermissionAssociation(id);
        roleDao.deleteUserAssociation(id);
        roleDao.deleteById(id);

    }

    //分页查询
    public PageResult findPage(QueryPageBean pageBean){
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }
    //根据id查找角色基本信息
    public Role findById (Integer id){
        return roleDao.findById(id);
    }
    //编辑角色信息
    public void edit (Role role, Integer[] userIds, Integer[] permissionIds, Integer[] menuIds){
        roleDao.edit(role);
        //清理三个中间表的关系
        roleDao.deleteUserAssociation(role.getId());
        roleDao.deletePermissionAssociation(role.getId());
        roleDao.deleteMenuAssociation(role.getId());
        //重新建立三个关联关系
        this.setRoleAndUser(role.getId(),userIds);
        this.setRoleAndMenu(role.getId(),menuIds);
        this.setRoleAndPermission(role.getId(),permissionIds);
    }

    //设置角色和菜单的多对多关系
    private void setRoleAndMenu (Integer roleId, Integer[]menuIds){
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("menuId", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }
    //设置角色和权限的多对多关系
    private void setRoleAndPermission (Integer roleId, Integer[]permissionIds){
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }
    //设置角色和用户多对多关系.
    private void setRoleAndUser (Integer roleId, Integer[]userIds){
        if (userIds != null && userIds.length > 0) {
            for (Integer userId : userIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("userId", userId);
                roleDao.setRoleAndUser(map);
            }
        }
    }
}
